﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace OkTool.Util
{
    public class ClassUtil
    {
        /// <summary>
        /// 安全的获取对象类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Type GetType<T>(T obj)
        {
            return obj?.GetType();
        }

        /// <summary>
        /// 获取当前type实现和继承的所有接口(父类)
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Type[] GetInterfaces<T>(Type type)
        {
            return type.GetInterfaces();
        }

        /// <summary>
        /// 是否为顶层类,即定义在命名空间中的类,而非定义在类中的内部类
        /// </summary>
        /// <returns></returns>
        public static bool IsTopLevelClass(Type type)
        {
            if (type == null) return false;
            return !type.IsNested;
        }

        /// <summary>
        /// 获取类名
        /// <para>类名并不包含.cs这个扩展名</para>
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="isSimple">是否简单的类名,如果为true,返回不带命名空间的类名</param>
        /// <returns></returns>
        public static string GetClassName(object obj, bool isSimple)
        {
            if (obj == null) return null;
            var type = obj.GetType();
            return isSimple ? type.FullName : type.Name;
        }

        /// <summary>
        /// 转换成DataTable
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="items"></param>
        /// <returns></returns>
        public static DataTable ToDataTable<T>(List<T> items)
        {
            if (items == null)
            {
                return null;
            }
            DataTable dataTable = new DataTable(typeof(T).Name);

            // 创建表格的列
            foreach (var propertyInfo in typeof(T).GetProperties())
            {
                dataTable.Columns.Add(propertyInfo.Name, propertyInfo.PropertyType);
            }

            // 填充表格的数据
            foreach (T item in items)
            {
                var values = new object[dataTable.Columns.Count];
                for (int i = 0; i < dataTable.Columns.Count; i++)
                {
                    values[i] = typeof(T).GetProperty(dataTable.Columns[i].ColumnName)?.GetValue(item);
                }
                dataTable.Rows.Add(values);
            }

            return dataTable;
        }

        /// <summary>
        /// 获的指定类中的Public方法名
        /// </summary>
        /// <param name="type">类</param>
        /// <returns></returns>
        public static HashSet<string> GetPublicMethodNames(Type type)
        {
            return ReflectUtil.GetPublicMethodNames(type);
        }

        /// <summary>
        /// 获得指定类包含某个特性的Public方法列表
        /// </summary>
        /// <param name="type">类</param>
        /// <param name="attributeType"></param>
        /// <returns></returns>
        public static List<MethodInfo> GetPublicMethodsByAttribute(Type type, Type attributeType)
        {
            return ReflectUtil.GetPublicMethodsByAttribute(type,attributeType);
        }
    }
}
